﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Catering_DBOpertion;
using System.Collections.Generic;
using Catering_Model;//Please add references
namespace Catering_Operation
{
	/// <summary>
	/// 数据访问类:Dish
	/// </summary>
	public partial class D_Dish
	{
		public D_Dish()
		{}
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Dish");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(E_Dish model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Dish(");
            strSql.Append("AreaIDS,Name,Characteristic,Back,Picture,FMID,DTID,Method,IsStuffing)");
            strSql.Append(" values (");
            strSql.Append("@AreaIDS,@Name,@Characteristic,@Back,@Picture,@FMID,@DTID,@Method,@IsStuffing)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@AreaIDS", SqlDbType.NVarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Characteristic", SqlDbType.Text),
					new SqlParameter("@Back", SqlDbType.Text),
					new SqlParameter("@Picture", SqlDbType.VarChar,200),
					new SqlParameter("@FMID", SqlDbType.Int,4),
					new SqlParameter("@DTID", SqlDbType.Int,4),
					new SqlParameter("@Method", SqlDbType.Text),
					new SqlParameter("@IsStuffing", SqlDbType.Int,4)};
            parameters[0].Value = model.AreaIDS;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Characteristic;
            parameters[3].Value = model.Back;
            parameters[4].Value = model.Picture;
            parameters[5].Value = model.FMID;
            parameters[6].Value = model.DTID;
            parameters[7].Value = model.Method;
            parameters[8].Value = model.IsStuffing;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                model.id = Convert.ToInt32(obj);
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(E_Dish model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Dish set ");
            strSql.Append("AreaIDS=@AreaIDS,");
            strSql.Append("Name=@Name,");
            strSql.Append("Characteristic=@Characteristic,");
            strSql.Append("Back=@Back,");
            strSql.Append("Picture=@Picture,");
            strSql.Append("FMID=@FMID,");
            strSql.Append("DTID=@DTID,");
            strSql.Append("Method=@Method,");
            strSql.Append("IsStuffing=@IsStuffing");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@AreaIDS", SqlDbType.NVarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Characteristic", SqlDbType.Text),
					new SqlParameter("@Back", SqlDbType.Text),
					new SqlParameter("@Picture", SqlDbType.VarChar,200),
					new SqlParameter("@FMID", SqlDbType.Int,4),
					new SqlParameter("@DTID", SqlDbType.Int,4),
					new SqlParameter("@Method", SqlDbType.Text),
					new SqlParameter("@IsStuffing", SqlDbType.Int,4),
					new SqlParameter("@id", SqlDbType.Int,4)};
            parameters[0].Value = model.AreaIDS;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Characteristic;
            parameters[3].Value = model.Back;
            parameters[4].Value = model.Picture;
            parameters[5].Value = model.FMID;
            parameters[6].Value = model.DTID;
            parameters[7].Value = model.Method;
            parameters[8].Value = model.IsStuffing;
            parameters[9].Value = model.id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Dish ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool DeleteList(string idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Dish ");
            strSql.Append(" where id in (" + idlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public E_Dish GetModel(int id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 id,AreaIDS,Name,Characteristic,Back,Picture,FMID,DTID,Method,IsStuffing from Dish ");
            strSql.Append(" where id=@id");
            SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
};
            parameters[0].Value = id;

            E_Dish model = new E_Dish();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["id"].ToString() != "")
                {
                    model.id = int.Parse(ds.Tables[0].Rows[0]["id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["AreaIDS"] != null)
                {
                    model.AreaIDS = ds.Tables[0].Rows[0]["AreaIDS"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Name"] != null)
                {
                    model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Characteristic"] != null)
                {
                    model.Characteristic = ds.Tables[0].Rows[0]["Characteristic"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Back"] != null)
                {
                    model.Back = ds.Tables[0].Rows[0]["Back"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Picture"] != null)
                {
                    model.Picture = ds.Tables[0].Rows[0]["Picture"].ToString();
                }
                if (ds.Tables[0].Rows[0]["FMID"].ToString() != "")
                {
                    model.FMID = int.Parse(ds.Tables[0].Rows[0]["FMID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["DTID"].ToString() != "")
                {
                    model.DTID = int.Parse(ds.Tables[0].Rows[0]["DTID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Method"] != null)
                {
                    model.Method = ds.Tables[0].Rows[0]["Method"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsStuffing"].ToString() != "")
                {
                    model.IsStuffing = int.Parse(ds.Tables[0].Rows[0]["IsStuffing"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select id,AreaIDS,Name,Characteristic,Back,Picture,FMID,DTID,Method,IsStuffing ");
            strSql.Append(" FROM Dish ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" id,AreaIDS,Name,Characteristic,Back,Picture,FMID,DTID,Method,IsStuffing ");
            strSql.Append(" FROM Dish ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            SqlParameter[] parameters = {
                    new SqlParameter("@tblName", SqlDbType.VarChar, 255),
                    new SqlParameter("@fldName", SqlDbType.VarChar, 255),
                    new SqlParameter("@PageSize", SqlDbType.Int),
                    new SqlParameter("@PageIndex", SqlDbType.Int),
                    new SqlParameter("@IsReCount", SqlDbType.Bit),
                    new SqlParameter("@OrderType", SqlDbType.Bit),
                    new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
                    };
            parameters[0].Value = "Dish";
            parameters[1].Value = "id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method

        #region 扩展方法

        List<string> sql = new List<string>();
        public int Addd(Catering_Model.E_Dish model)
        {
            SqlParameter[] parameters = {
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Characteristic", SqlDbType.Text),
					new SqlParameter("@FMID", SqlDbType.Int),
					new SqlParameter("@DTID", SqlDbType.Int),
					new SqlParameter("@Picture", SqlDbType.VarChar,50),				
					new SqlParameter("@Method", SqlDbType.Text),
					};
            parameters[0].Value = model.Name;
            parameters[1].Value = model.Characteristic;
            parameters[2].Value = model.FMID;
            parameters[3].Value = model.DTID;
            parameters[4].Value = model.Picture;
            parameters[5].Value = model.Method;

            SqlDataReader read = DbHelperSQL.RunProcedure("DishCCGC", parameters);
            int id = 0;
            while (read.Read())
            {
                id = int.Parse(read["name"].ToString());
            }
            read.Close();
            return id;
        }

        /// <summary>
        /// 获取总记录数
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <returns>返回记录数</returns>
        public int GetCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(*)  FROM Dish ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return Convert.ToInt32(DbHelperSQL.ExecuteScalar(strSql.ToString()));
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere, string field)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select " + field);
            strSql.Append(" FROM Dish ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 更新菜品对应的区域ID 集合
        /// </summary>
        /// <param name="DishID"></param>
        /// <returns></returns>
        public bool UpdateAreaIDByDishID(int DishID)
        {
            SqlParameter[] parm = new SqlParameter[]{
                new SqlParameter("@DishID",DishID)
            };
            DataTable AreaDt = DbHelperSQL.RunProcedureReDataTable("Proc_DishForSelectAreaID", parm);
            E_Dish eDish = this.GetModel(DishID);
            if (AreaDt != null && AreaDt.Rows.Count > 0)
            {
                string AreaIDS = "";
                for (int i = 0; i < AreaDt.Rows.Count; i++)
                {
                    AreaIDS += "," + AreaDt.Rows[i]["AreaID"].ToString();
                }
                if (AreaIDS.Length > 0)
                {
                    AreaIDS = AreaIDS + ",";
                    eDish.AreaIDS = AreaIDS;
                }
                else
                {
                    eDish.AreaIDS = "";
                }
                this.Update(eDish);
            }
            return true;
        }
        #endregion
    }
}

